﻿using AutoMapper;
using MediatR;
using T_Rbac_APi.Application.Command.Jobs;
using T_Rbac_Demain;
using T_Rbac_Rep.Interface;

namespace T_Rbac_APi.Application.CommandHandler.JobsHandler
{
    public class JobUpdateListHandlers : IRequestHandler<JobUpdateCommand, int>
    {
        private readonly IMapper mapper;
        private readonly IjobRep ijobRep;

        public JobUpdateListHandlers(IMapper mapper, IjobRep ijobRep)
        {
            this.mapper = mapper;
            this.ijobRep = ijobRep;
        }

        public async Task<int> Handle(JobUpdateCommand request, CancellationToken cancellationToken)
        {
            var lista = await ijobRep.GetAloneByresult(x => x.Jname == request.Jname&&!x.IsDel&&x.Jid!=request.Jid);
            if (lista!=null)
            {
                return await Task.FromResult(-2);
            }
            request.UpdateBy = "小红";
            request.UpdateDate = DateTime.Now;
            var list= mapper.Map<Job>(request);
            return await ijobRep.Update(list);
        }
    }
}
